<?php
declare (strict_types = 1);

namespace app\admin\controller\erp;

use app\admin\controller\AuthController;
use app\admin\model\erp\ErpSubjects;
use app\admin\model\system\SystemMenus;
use think\facade\Config;
use think\facade\Db;
use think\Request;

class BasicsSubject extends AuthController
{
    /**
     * 显示列表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        $data = ErpSubjects::typeAll();

        $sonAuth = SystemMenus::getSonAuth($req->controller(), $req->action(),array_keys($this->auth));
        // 显示页面
        $this->assign('sonAuth', $sonAuth);
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 创建资源.
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function add(Request $req)
    {
        $id = intval($req->param('id/d'));
        if(!$id) $this->error('无效参数','', -1);
        $sub = ErpSubjects::find($id);
        if(!$sub) $this->error('无效参数','', -1);
        $level = $sub['level'] + 1;
        $code = ErpSubjects::where('code','like', "{$sub['code']}%")->where('level',$level)->max('code');
        if($code <= $sub['code']) $code = $sub['code'] . '000';
        $code = $code + 1;
        if($req->isAjax()) {
            if(in_array($sub['code'], ['1006','1008','2003','2009','1005','2004'])) {
                $item = [
                    'rel_id'    => $req->param('user_id/d',0),
                    'like'      => $req->param('like',[]),
                    'sys'       => $req->param('sys/d',0),
                ];
                if(!$item['rel_id']) $this->error('请选择往来客户');
                if(empty($item['like'])) $this->error('请选择科目');
                $res = ErpSubjects::addCode($item);
                if($res && !empty($res)) {
                    return json(['code'=>2,'msg'=>'添加成功','data'=>$res]);
                } else {
                    $this->error();
                }
            } else {
                $item = [
                    'pid'       => $id,
                    'name'      => $req->param('name',''),
                    'sys'       => $req->param('sys/d',0),
                    'remark'    => $req->param('remark',''),
                    'pname'     => $sub['level']==0 ? $sub['name'] : $sub['pname'],
                    'level'     => $level,
                    'edit_luck' => $level > 1 ? 0 : 1,
                    'update_at' => time()
                ];
                if(!$item['name']) $this->error('请设置科目名称');
                if(ErpSubjects::where('code',$code)->find()) $this->error('科目编码已存在');
                $item['code'] = $code;
                if($res = ErpSubjects::create($item)) {
                    return json(['code'=>1,'msg'=>'添加成功','data'=>$res]);
                } else {
                    $this->error();
                }
            }
        }
        $this->assign('sub',$sub);
        $this->assign('code',$code);

        if(in_array($sub['code'], ['1006','2003','1008','2009','1005','2004'])) {
            $user_types = Config::get('erp.user_types');
            $users = Db::name('erp_vendors')->column('id,name,phone,type', 'id');
            $this->assign('users', $users);
            $this->assign('user_types', $user_types);
            return $this->fetch('add_user');
        }
        return $this->fetch();
    }

    /**
     * 更新资源
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function edit(Request $req)
    {
        $id = intval($req->param('id/d'));
        if(!$id) $this->error('无效参数','', -1);
        $sub = ErpSubjects::find($id);
        if(!$sub) $this->error('无效参数','', -1);
        if($req->isAjax()) {
            $item = [
                'name'      => $req->param('name',''),
                'sys'       => $req->param('sys/d',0),
                'remark'    => $req->param('remark',''),
                'update_at' => time()
            ];
            if(!$item['name']) $this->error('请设置科目名称');

            if(ErpSubjects::where('id',$id)->update($item)) {
                $data = ErpSubjects::find($id);
                return json(['code'=>1,'msg'=>'添加成功','data'=>$data]);
            } else {
                $this->error();
            }
        }
        $this->assign('sub',$sub);
        return $this->fetch();
    }

    /*
     * ajax获取客户对应code
     */
    public function public_user_code(Request $req)
    {
        $id = intval($req->param('id/d'));
        //ca=1006,cb=2003,cc=1008,cd=2009
        $data = ['ca'=>0,'cb'=>0,'cc'=>0,'cd'=>0,'ce'=>0,'cf'=>0];
        if($id && $codes = ErpSubjects::where('rel_id',$id)->column('id,code,name','code')) {
            foreach($codes as $val) {
                $fcode = substr($val['code'],0,4);
                if($fcode == '1006') $data['ca'] = 1;
                if($fcode == '2003') $data['cb'] = 1;
                if($fcode == '1008') $data['cc'] = 1;
                if($fcode == '2009') $data['cd'] = 1;
                if($fcode == '1005') $data['ce'] = 1;
                if($fcode == '2004') $data['cf'] = 1;
            }
        }
        return json(['code'=>1,'data'=>$data]);
    }

    /*
     * ajax获取科目Tree
     */
    public function public_get_tree()
    {
        $data = ErpSubjects::tidySelect();
        return json($data);
    }
    /*
     * ajax获取科目加长Tree
     */
    public function public_code_tree()
    {
        $data = ErpSubjects::tidyCodeSelect();
        return json($data);
    }
}
